package com.eleadmin.order.controller;

import com.eleadmin.damon.utils.results.PageResult2;
import com.eleadmin.common.core.web.*;
import com.eleadmin.common.core.annotation.OperationLog;
import com.eleadmin.common.system.entity.User;
import com.eleadmin.order.service.UserService2;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.apache.poi.ss.formula.functions.T;


/**
 *
 * 用户
 * @author : Doman
 * @date : 2023-04-21 10:42:44
 * @version ：1.0
 */

@Api(tags = "用户")
@RestController
@RequestMapping("/api/order/user")
public class UserController2 extends BaseController {

    @Autowired
    private UserService2 userService;

    @PreAuthorize("hasAuthority('order:user:list')")
    @GetMapping("/page")
    public JsonResult page(PageResult2<T> page, User user) throws Exception{
        return userService.list(page, user, getLoginUser());
    }

    @PreAuthorize("hasAuthority('order:user:list')")
    @ApiOperation("查询全部用户")
    @GetMapping()
    public JsonResult list(User user){
        return userService.list(user, getLoginUser());
    }

    @PreAuthorize("hasAuthority('order:user:list')")
    @ApiOperation("根据id查询用户")
    @GetMapping("/{id}")
    public JsonResult get(@PathVariable("id") String id) {
        return userService.getById(id);
    }


    @PreAuthorize("hasAuthority('order:user:save')")
    @OperationLog
    @ApiOperation(value ="添加用户")
    @PostMapping()
    public JsonResult save(@RequestBody User user) {
        try {
            return userService.save(user, getLoginUser());
        } catch (Exception e) {
            return JsonResult.error(e.getMessage());
        }

    }

    @PreAuthorize("hasAuthority('order:user:update')")
    @OperationLog
    @ApiOperation(value ="修改用户")
    @PutMapping()
    public JsonResult update(@RequestBody User user) {
        try {
            return userService.update(user, getLoginUser());
        } catch (Exception e) {
            return JsonResult.error(e.getMessage());
        }
    }

    @PreAuthorize("hasAuthority('order:user:remove')")
    @OperationLog
    @ApiOperation(value ="删除用户")
    @DeleteMapping("/{id}")
    public JsonResult remove(@PathVariable("id") String id) {
        try {
            return userService.remove(id, getLoginUser());
        } catch (Exception e) {
            return JsonResult.error(e.getMessage());
        }
    }

}
